package caddey.jonathan.gameframeworkandroidimplementation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import framework.Color;
import framework.GraphicsWrapper;
import framework.ImageTinter;
import framework.ImageWrapper;
import framework.Rectangle;

public class AndroidGraphics implements GraphicsWrapper {

	private Canvas my_canvas;
	private Paint my_paint;
	
	private static int toARGB(final Color the_color) {
		return android.graphics.Color.argb(
				the_color.getAlpha(), the_color.getRed(), the_color.getGreen(),
				the_color.getBlue());
	}
	
	public AndroidGraphics(final Bitmap the_bitmap) {
		my_paint = new Paint();
		my_canvas = new Canvas(the_bitmap);
	}
	public void setCanvas(final Canvas the_canvas) {
		my_canvas = the_canvas;
	}
	
	@Override
	public void setColor(Color the_color) {
		my_paint.setColor(toARGB(the_color));
		
	}
	

	@Override
	public void fillRect(int the_x, int the_y, int the_width, int the_height) {
		Rect rect = new Rect(the_x, the_y, the_x + the_width, the_y + the_height);
		my_canvas.drawRect(rect, my_paint);
		
	}

	@Override
	public void fillOval(int the_x, int the_y, int the_width, int the_height) {
		RectF rect = new RectF(the_x, the_y, the_x + the_width, the_y + the_height);
		my_canvas.drawOval(rect, my_paint);
		
	}

	@Override
	public Rectangle getStringBounds(String the_string) {
		Rect rect = new Rect();
		my_paint.getTextBounds(the_string, 0, the_string.length(), rect);
		return new Rectangle(rect.width(), rect.height());
	}

	@Override
	public void drawString(String the_string, int the_x, int the_y) {
		my_canvas.drawText(the_string, the_x, the_y, my_paint);
		
	}

	@Override
	public void translate(double the_x, double the_y) {
		my_canvas.translate((float) the_x, (float) the_y);
	}

	@Override
	public void scale(double the_x, double the_y) {
		my_canvas.scale((float)the_x, (float)the_y);
	}

	@Override
	public void saveMatrix() {
		my_canvas.save();
	}

	@Override
	public void restoreMatrix() {
		my_canvas.restore();
	}

	@Override
	public int getWidth() {
		return my_canvas.getWidth();
	}

	@Override
	public int getHeight() {
		return my_canvas.getHeight();
	}
	
	@Override
	public ImageWrapper newImage(final int the_width, final int the_height) {
		return new AndroidImage(the_width, the_height);
	}
	
	@Override
	public void drawImage(ImageWrapper the_image, final int the_x, final int the_y) {
		my_canvas.drawBitmap(((AndroidImage) the_image).getBitmap(), the_x, the_y, my_paint);
		
	}
	@Override
	public ImageTinter newImageTinter(final Color the_color) {
		return new AndroidImageTinter(the_color);
	}
	@Override
	public void dispose() {
	}
	@Override
    public void clear() {
		/*
		 * This could be in image but it was easier to do it in graphics
		 * to keep consistency with the java framework.
		 * https://groups.google.com/d/msg/android-developers/KL583Qq5uYI/btKQCxO6NNYJ
		 */
    	my_canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    }
	public class AndroidImageTinter implements ImageTinter {
		private final ColorFilter my_filter;
		public AndroidImageTinter(final Color the_color) {
			my_filter = new PorterDuffColorFilter(toARGB(the_color), PorterDuff.Mode.MULTIPLY);
		}

		@Override
		public void drawTintedImage(final GraphicsWrapper the_graphics, ImageWrapper the_image, int the_x, int the_y) {
			ColorFilter old = my_paint.getColorFilter();
			final AndroidGraphics g = (AndroidGraphics) the_graphics;
			g.my_paint.setColorFilter(my_filter);
			g.drawImage(the_image, the_x, the_y);
			g.my_paint.setColorFilter(old);
		}
		
	}


}
